package com.aaa.controller;

import com.aaa.entity.AlertCode;
import com.aaa.entity.Dept;
import com.aaa.entity.Userinfo;
import com.aaa.service.UserinfoService;
import com.aaa.util.ImgLoad;
import com.aaa.util.MyUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.net.URL;
import java.util.*;

/**
 * @author ：zhouhangyong
 * @date ：Created in 2022/6/22 14:37
 * @description：
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("userinfo")
public class UserInfoController {
    AlertCode alertCode;
    @Resource
    UserinfoService userinfoService;
    static PutObjectRequest putObjectRequest;
    /**
     * @Author zhouhangyong
     * @Description 显示所有的userinfo数据//TODO
     * @Date 17:10 2022/6/22
     * @Param request
     * @return AlertCode
     **/
    @GetMapping("showUserinfo.do")
    public AlertCode showUserinfo(HttpServletRequest request){
        Map<String, Object> namesUtil = MyUtil.parameterNamesUtil(request, null);
        Page userinfoList = userinfoService.userinfoList(namesUtil);
//        alertCode.setCode(0);
//        alertCode.setCount(userinfoList.getTotal());
//        alertCode.setData(userinfoList.getResult());
        AlertCode alertCode = new AlertCode(0,"",userinfoList.getTotal(),userinfoList.getResult());
        System.out.println("showUserinfo" + alertCode.toString());
        return alertCode;
    }
    /**
     * @Author zhouhangyong
     * @Description 显示所有的部门//TODO
     * @Date 17:10 2022/6/22
     * @Param
     * @return AlertCode
     **/
    @GetMapping("deptShow.do")
    public AlertCode deptShow(){
        alertCode = userinfoService.deptList();
        return alertCode;
    }
    /**
     * @Author zhouhangyong
     * @Description 显示所有的部门//TODO
     * @Date 17:10 2022/6/22
     * @Param userinfo
     * @return AlertCode
     **/
    @PostMapping("addUserinfo.do")
    public AlertCode addUserinfo(Userinfo userinfo){
        String urlLoad = ImgLoad.urlLoad(putObjectRequest);
        userinfo.setAvatar(urlLoad);
        System.out.println("添加"+userinfo.toString());
        alertCode = userinfoService.addUserinfo(userinfo);
        return alertCode;
    }
    @GetMapping("updateKey.do")
    public AlertCode updateKey(Integer id){
        alertCode = userinfoService.updateServiceKey(id);
        return alertCode;
    }
    @PutMapping("updateUserinfo.do")
    public AlertCode updateUserinfo(Userinfo userinfo){
        String urlLoad = ImgLoad.urlLoad(putObjectRequest);
        userinfo.setAvatar(urlLoad);
        System.out.println(urlLoad);
        System.out.println(userinfo.toString());
        alertCode = userinfoService.updateUserinfo(userinfo);
        return alertCode;
    }
    @DeleteMapping("/{id}/deleteUserinAll.do")
    public AlertCode deleteUserinAll(@PathVariable(value = "id") String id){
        System.out.println(id);
        AlertCode deleteUserinfo = userinfoService.deleteUserinfo(id);
        return deleteUserinfo;
    }
    @PutMapping("updatePass.do")
    public AlertCode updatePass(Userinfo userinfo){
        System.out.println(userinfo.toString());
        alertCode = userinfoService.updatePassword(userinfo);
        return alertCode;
    }



    @RequestMapping("upload.do")
    @ResponseBody
    public AlertCode upload(MultipartFile file, HttpServletRequest request){

        System.out.println(file);
        putObjectRequest= ImgLoad.imgLoad(file, false);
        if (putObjectRequest!= null){
            return new AlertCode(0,"上传成功");
        }else {
            return new AlertCode(1,"上传失败");
        }

    }
}
